package com.jvtd.eyl.ui.main.home.periodicalInfo;

import com.jvtd.eyl.base.BasePresenter;
import com.jvtd.eyl.bean.binding.BookBean;
import com.jvtd.eyl.bean.binding.ClassBean;
import com.jvtd.eyl.bean.binding.PeriodicalBean;
import com.jvtd.eyl.bean.http.TimeBean;
import com.jvtd.eyl.bean.ui.SortsBean;
import com.jvtd.eyl.data.DbManager;
import com.jvtd.eyl.rxjava.JvtdRxSchedulers;
import com.jvtd.rxjava.JvtdObserverSubscriber;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.disposables.CompositeDisposable;

/**
 * 期刊详情
 * <p>
 * 作者:chenlei
 * 时间:2018/11/14 9:18 AM
 */
public class PeriodicalInfoPresenter<V extends PeriodicalInfoMvpView> extends BasePresenter<V>
        implements PeriodicalInfoMvpPresenter<V> {

    @Inject
    public PeriodicalInfoPresenter(DbManager dbManager, CompositeDisposable disposable) {
        super(dbManager, disposable);
    }

    @Override
    public void getTimeClassList(String id) {
        if (!isAttachView()) return;
        getCompositeDisposable().add(getDbManager().getTimeClassList(id).compose(JvtdRxSchedulers.handleObservableResult())
                .subscribeWith(new JvtdObserverSubscriber<List<TimeBean>>(getMvpView()) {
                    @Override
                    public void onNext(List<TimeBean> timeBeans) {
                        super.onNext(timeBeans);
                        if (isAttachView()) {
                            getMvpView().getTimeClassListSuccess(timeBeans);
                        }
                    }
                }));
    }

    @Override
    public void getBookList(PeriodicalBean periodicalBean, SortsBean sortsBean, String year) {
        if (!isAttachView()) return;
        getCompositeDisposable().add(getDbManager().getBookList(periodicalBean.getId(),getCurrentUser().getUserId(),sortsBean.getSortsId(),year)
                .compose(JvtdRxSchedulers.handleObservableResult())
                .subscribeWith(new JvtdObserverSubscriber<List<BookBean>>(getMvpView())
                {
                    @Override
                    public void onNext(List<BookBean> bookBeans)
                    {
                        super.onNext(bookBeans);
                        if (isAttachView())
                            getMvpView().getBookListSuccess(bookBeans);
                    }

                    @Override
                    public void onError(Throwable e)
                    {
                        super.onError(e);
                        if (isAttachView())
                            getMvpView().getBookListFailed();
                    }
                }));
    }

    @Override
    public void addBook(List<BookBean> bookBeans) {
        if (isAttachView())
            getMvpView().showLoading();
        super.addBook(bookBeans);
    }
}
